﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using ITProject.Domain.Entities;

namespace ITProject.WebUI.Models.ProjectUser
{
    public class ProjectUserScenariosViewModel
    {
        public string Text { get; set; }
        public string Prefix { get; set; }
        public List<ProjectUserScenariosViewModel> Extensions { get; set; }
        public ProjectUserScenariosViewModel Parent { get; set; }
    }
    
    public class ProjectUserFunctionalRequirementViewModel
    {
        [Display(Name="Nazwa")]
        public string Name { get; set; }
        [Display(Name = "Aktorzy")]
        public string Actors { get; set; }
        [Display(Name = "Priorytet")]
        public string Priority { get; set; }
        [Display(Name = "Autor")]
        public string Author { get; set; }
        [Display(Name = "Data utworzenia")]
        public string DateOfCreate { get; set; }
        [Display(Name = "Data modyfikacji")]
        public string DateOfLastModification { get; set; }
        [Display(Name = "Opis")]
        public string Description { get; set; }
        [Display(Name = "Warunki początkowe")]
        public string InitConditions { get; set; }
        [Display(Name = "Warunki końcowe")]
        public string FinalConditions { get; set; }

        [Display(Name = "Scenariusze")]
        public List<ProjectUserScenariosViewModel> Scenarios { get; set; }

        public static ProjectUserFunctionalRequirementViewModel Convert(FunctionalRequirement fr, string author, Scenario[] scs)
        {
            ProjectUserFunctionalRequirementViewModel vm = new ProjectUserFunctionalRequirementViewModel();
            vm.Name = fr.Name;
            vm.Priority = PriorityViewModel.ConvertFromByte(fr.Priority);
            vm.InitConditions = fr.InitConditions;
            vm.FinalConditions = fr.FinalConditions;
            vm.DateOfCreate = fr.DateOfCreate.ToString();
            vm.DateOfLastModification = fr.DateOfCreate.ToString();
            vm.Actors = fr.Actors;
            vm.Description = fr.Description;
            vm.Author = author;
            vm.Scenarios = new List<ProjectUserScenariosViewModel>();
            CompleteScenarios(vm.Scenarios,scs);
            return vm;
        }

        private static void CompleteScenarios(List<ProjectUserScenariosViewModel> scenarios,Scenario[] scs,int? parentId=null, string prefix="", ProjectUserScenariosViewModel parent=null)
        {
            bool added = true;
            int number = 1;
            char startChar='A';
            while (added)
            {
                added = false;
                Scenario newSc = scs.Where(x => x.ParentId == parentId && x.Number == number).FirstOrDefault();
                if (newSc != null)
                {
                    added = true;
                    ProjectUserScenariosViewModel vm = new ProjectUserScenariosViewModel();
                    if (prefix == "" || Char.IsLetter(prefix[prefix.Length - 2]))
                        vm.Prefix = prefix+number.ToString() + ".";
                    else
                        vm.Prefix = prefix+((char)((int)startChar + number - 1)).ToString() + ".";
                    vm.Text = newSc.Text;
                    vm.Extensions = new List<ProjectUserScenariosViewModel>();
                    vm.Parent = parent;
                    CompleteScenarios(vm.Extensions, scs, newSc.Id, vm.Prefix, vm);
                    scenarios.Add(vm);
                    number++;
                }
            }
        }
    }
}